package com.maxjay.component.metadata.model;

import java.util.ArrayList;
import java.util.List;

import com.maxjay.common.util.collection.CollectionUtil;
import com.maxjay.component.metadata.core.interceptor.EntityInterceptor;

public class DefaultEntity implements Entity {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7450323261991409889L;

	private String id; 
	
	private String name;
	
	private String tabName;
	
	private String namaCn;
	
	private EntityField keyField;
	
	private List<EntityField> entityFields = new ArrayList<EntityField>();
	
	private List<EntityInterceptor> entityInterceptors = new ArrayList<EntityInterceptor>(0);
	
	private EntityConfig entityConfig;
	
	public DefaultEntity() {
	}
	
	public DefaultEntity(String name, String nameCn) {
		this.name = name;
		this.namaCn = nameCn;
	}
	
	public DefaultEntity(String id, String name, String nameCn) {
		this.id = id;
		this.name = name;
		this.namaCn = nameCn;
	}
	
	public void addEntityField(EntityField field) {
		if(field.isKey()) {
			this.keyField = field;
		}
		this.entityFields.add(field);
	}
	
	@Override
	public EntityField getEntityFieldByName(String fieldName) {
		return CollectionUtil.getMatchedBean(entityFields, "name", fieldName);
	}

	@Override
	public EntityField getEntityFieldById(String fieldId) {
		return CollectionUtil.getMatchedBean(entityFields, "id", fieldId);
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public String getNameCn() {
		return this.namaCn;
	}
	
	@Override
	public String getTabName() {
		return tabName;
	}

	@Override
	public List<EntityField> getEntityFields() {
		return this.entityFields;
	}
	
	public void setKeyField(EntityField keyField) {
		this.keyField = keyField;
	}

	@Override
	public EntityField getKeyField() {
		return this.keyField;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setTabName(String tabName) {
		this.tabName = tabName;
	}

	@Override
	public List<EntityInterceptor> getEntityInterceptors() {
		return entityInterceptors;
	}
	
	public void setEntityInterceptors(List<EntityInterceptor> entityInterceptors) {
		this.entityInterceptors = entityInterceptors;
	}
	
	public void addEntityInterceptor(EntityInterceptor entityInterceptor) {
		this.entityInterceptors.add(entityInterceptor);
	}

	@Override
	public EntityConfig getEntityConfig() {
		return entityConfig;
	}
	
	public void setEntityConfig(EntityConfig entityConfig) {
		this.entityConfig = entityConfig;
	}

}
